package com.stevenmoon.csdnplus.presenter;

import com.stevenmoon.csdnplus.app.LoginPrefs;
import com.stevenmoon.csdnplus.app.UserDetailPrefs;
import com.stevenmoon.csdnplus.bean.AccountBean;
import com.stevenmoon.csdnplus.bean.CsdnIdentity;
import com.stevenmoon.csdnplus.bean.LoginReqBean;
import com.stevenmoon.csdnplus.bean.RankScoreChannel;
import com.stevenmoon.csdnplus.bean.ResponseResultBean;
import com.stevenmoon.csdnplus.bean.UserDetails;
import com.stevenmoon.csdnplus.bean.UserProfileReqBean;
import com.stevenmoon.csdnplus.fragment.LoginFragment;
import com.stevenmoon.csdnplus.logic.LoginManager;
import com.stevenmoon.csdnplus.logic.ProfileManager;
import com.stevenmoon.csdnplus.presenter.base.MvpBaseNoCheckPresenter;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WangJun on 2016/4/6.
 */
public class LoginPresenter extends MvpBaseNoCheckPresenter<LoginFragment> {


    public void doLogin(LoginReqBean bean) {

        Subscription s = LoginManager.doLogin(bean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new rx.Subscriber<ResponseResultBean<AccountBean>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showError(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseResultBean<AccountBean> resultBean) {
                        LoginPrefs.setSessionId(resultBean.sessionId);
                        LoginPrefs.setSessionExpired(resultBean.sessionExpired);
                        AccountBean accountBean = resultBean.data;
                        LoginPrefs.setTGC(accountBean.getTgc());
                        LoginPrefs.setUserInfo(accountBean.getUserInfo());
                        LoginPrefs.setMobile(accountBean.getMobile());
                        LoginPrefs.setEmail(accountBean.getEmail());
                        LoginPrefs.setUser_id(accountBean.getId());
                        LoginPrefs.setUser_userId(accountBean.getUserId());
                        LoginPrefs.setNickname(accountBean.getNickname());
                        LoginPrefs.setUserName(accountBean.getUserName());
                        //登录成功了，则去获取用户信息
                        fetchUserDetails(accountBean.getUserName());
                    }
                });
        getCompositeSubscription().add(s);

    }

    private void fetchUserDetails(final String username){
        UserProfileReqBean reqBean = new UserProfileReqBean();
        reqBean.setUsername(username);
        Subscription s = ProfileManager.getUserDetailInfo(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UserDetails>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showError(e.getMessage());
                    }

                    @Override
                    public void onNext(UserDetails userDetails) {
                        //保存到本地
                        UserDetailPrefs.setUserDetail(userDetails);
                        getIdentity(username);
                       // getView().onLoginSuccess();
                    }
                });
        getCompositeSubscription().add(s);
    }

    private void getIdentity(final String username) {
        Subscription s = ProfileManager.getUserIdentity(username)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CsdnIdentity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showError(e.getMessage());
                    }

                    @Override
                    public void onNext(CsdnIdentity uc) {
                        //TODO 保存UC数据到本地
                        //UserDetailPrefs.setUserDetail(userDetails);
                        getUC(username);
                    }
                });
        getCompositeSubscription().add(s);
    }

    private void getUC(String username){
        Subscription s = ProfileManager.getUC(username)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RankScoreChannel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showError(e.getMessage());
                    }

                    @Override
                    public void onNext(RankScoreChannel uc) {
                        //TODO 保存UC数据到本地
                        //UserDetailPrefs.setUserDetail(userDetails);
                        getView().onLoginSuccess();
                    }
                });
        getCompositeSubscription().add(s);
    }


}